<?php

/**
 * This is the model class for table "user".
 *
 * The followings are the available columns in table 'user':
 * @property integer $id
 * @property integer $name
 * @property integer $pass
 * @property string $role
 */
class LoginForm extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Account the static model class
	 */
	public $repass;
	public $rememberMe;
	public $_identity;
	
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'users';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, username, password, repass, email', 'required'),
			// username is unique
			array('username','unique'),
			// rememberMe needs to be a boolean
			array('rememberMe', 'boolean'),
			// compare repass with password
			array('repass','compare','compareAttribute'=>'password'),
			array('username','filter','filter'=>'strtolower'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, username, password, role', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'userLogs' => array(self::HAS_MANY, 'UserLogs', 'user_id'),
			'userProfiles' => array(self::HAS_MANY, 'UserProfiles', 'user_id'),
			'usergroups' => array(self::MANY_MANY, 'Usergroups', 'user_usergroup_map(user_id, group_id)'),
		);
	}
	protected function beforeSave()
	{
		if($this->isNewRecord)
		{
			$this->password = md5($this->password);
		
		}
		return true;
	}
	
	/**
	 * Authenticates the password.
	 * This is the 'authenticate' validator as declared in rules().
	 */
	public function authenticate($attribute,$params)
	{
		if(!$this->hasErrors())
		{
			$this->_identity=new UserIdentity($this->username,$this->password);
			if(!$this->_identity->authenticate())
				$this->addError('password','Incorrect username or password.');
		}
	}
	
	public function login($username_re, $password_re)
	{
		if($this->_identity===null)
		{
			$this->_identity=new UserIdentity($username_re, $password_re);
			$this->_identity->authenticate();
		}
		
		if($this->_identity->errorCode === UserIdentity::ERROR_NONE)
		{
			$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
			Yii::app()->user->login($this->_identity,$duration);
			LoginForm::model()->updateByPk($this->_identity->id, array('lastvisitDate'=> new CDbExpression('NOW()'),'registerDate'=> new CDbExpression('NOW()')));
			return true;
		}
		else
		{
			return false;
		}
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'Id',
			'name' => 'Your Name',
			'username' => 'Username',
			'password' => 'Password',
			'repass' => 'Re-Password',
			'email' => 'Email',
			'rememberMe'=>'Remember next to me',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);

		$criteria->compare('name',$this->name);
		
		$criteria->compare('username',$this->username);

		$criteria->compare('password',$this->pass);

		return new CActiveDataProvider('LoginForm', array(
			'criteria'=>$criteria,
		));
	}
}